const app = getApp()
Component({
  properties: {
    jianpan: {
      type: Boolean,
      value: true,
      observer(newVal, oldVal, changedPath) {
        this.setData({
          keyboardShow: newVal,
          jianp1: newVal
        })
      }
    },
    xinny: {
      type: Boolean,
      value: true,
      observer(newVal, oldVal, changedPath) {
        this.data.cphao.length = newVal ? 8 : 7
        this.setData({
          xingyC: newVal
        })
      }
    },
    dcphao: {
      type: String,
      value: '',
      observer(newVal, oldVal, changedPath) {
        let val = newVal
        let cphao = []
        if (val.length > 0) {
          cphao = newVal.split('')
        } else {
          // 处理数组中的undefined
          let arr = this.data.cphao
          for (let i = 0, len = arr.length; i < len; i++) {
            if (typeof arr[i] === 'undefined') {
              arr[i] = ''
            }
          }
          cphao = this.data.cphao.map(item => '')
        }
        this.setData({
          cphao
        })
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    array1: [
      '京', '津', '渝', '沪', '冀', '晋', '辽', '吉', '黑', '苏',
      '浙', '皖', '闽', '赣', '鲁', '豫', '鄂', '湘', '粤', '琼',
      '川', '贵', '云', '陕', '甘', '青', '蒙', '桂', '宁', '藏',
      '新', '使', '领', '警', '学', '港', '澳',
    ],
    array2: ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'],
    array3: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
    jianp1: false,
    jianp2: false,
    current: 0,
    cphao: [],
    xingyC: true,
    xuanZ: null,
    keyboardShow:false,
    isIpx: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    fangfa1(e) {
      this.setData({
        xuanZ: e.target.dataset.id
      })
    },
    fangfa2(e) {
      this.setData({
        xuanZ: null
      })
    },
    parkX1(e) {
      let cphao = this.data.cphao;
      let current = parseInt(this.data.current);
      let jianp1 = false;
      let jianp2 = false;
      cphao[current - 1] = e.target.dataset.id
      current++;
      if (current == 1) {
        jianp1 = true;
      } else {
        jianp2 = true;
      }
      if (this.data.xingyC) {
        if (current > 8) {
          jianp1 = false;
          jianp2 = false;
        }
      } else {
        if (current > 7) {
          jianp1 = false;
          jianp2 = false;
        }
      }
      if (cphao[0] == '') {
        current = 1;
        jianp1 = true;
        jianp2 = false;
      } else if (cphao[1] == '') {
        current = 2;
        jianp1 = false;
        jianp2 = true;
      }
      this.setData({
        jianp1: jianp1,
        jianp2: jianp2,
        current: current,
        cphao: cphao
      })
      this.triggerEvent('change', cphao)
    },
    parkSf(e) {
      let current = parseInt(e.currentTarget.dataset.id);
      let jianp1 = false;
      let jianp2 = false;
      let keyboardShow = true;
      if (current == 1) {
        jianp1 = true;
      } else {
        jianp2 = true;
      }
      this.setData({
        jianp1,
        jianp2,
        keyboardShow,
        current
      })
    },
    padrClose() {
      this.setData({
        jianp1: false,
        jianp2: false,
      })
    },
    parkSc() {
      let current = this.data.current;
      let cphao = this.data.cphao;
      cphao[current - 1] = '';
      let jianp1 = false;
      let jianp2 = false;
      current--;
      if (current <= 1) {
        current = 1;
        jianp1 = true;
      } else {
        jianp2 = true;
      }
      this.setData({
        jianp1: jianp1,
        jianp2: jianp2,
        current: current,
        cphao: cphao
      })
      this.triggerEvent('change', cphao)
    },
    checkCardNo(plateNum) {
      var re = /^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{5,6}$/;
      if (!re.test(plateNum)) {
        wx.showToast({ title: '输入的车牌号格式不正确' + plateNum, duration: 2000 });
        return false;
      } else {
        return true;
      }
    }
  },
  attached () {
    this.data.isIpx = /iphone\sx/i.test(app.globalData.phone.toLowerCase())
    this.setData({
      isIpx: this.data.isIpx
    })
  }
})
